﻿using System;
using System.IO;
using System.Data;
using System.Web.UI.WebControls;

namespace HiauPdm.Bll
{
    /// <summary>
    /// 外购件编辑
    /// </summary>
    public class BWgjBj
    {
        #region 外购件编辑

        /// <summary>
        /// 获得外购件编辑总记录数
        /// </summary>
        public static DataSet GetWgjBjCount(string sWhere, string sDh)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                return da.ExecuteQuery(string.Format("SELECT COUNT(1) FROM 购件目录 WHERE 状态=0 AND (@P0='' OR 代号 LIKE @P0+'%') AND {0}", BGlobal.BuildLike(sWhere, "代号", "名称", "规格型号")), sDh);
            }
        }

        /// <summary>
        /// 获得外购件编辑
        /// </summary>
        public static DataSet GetWgjBj(string sWhere, string sDh, int iPageSize, int iPageIndex)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                return da.ExecuteQuery(string.Format("SELECT TOP {0} A.ID,A.代号,A.类别,A.名称,A.规格型号,A.单位,A.品牌厂家,A.编辑人,(SELECT COUNT(1) FROM 购件文档 B WHERE A.代号 LIKE B.购件代号+'%') 文档 FROM 购件目录 A WHERE A.状态=0 AND (@P0='' OR A.代号 LIKE @P0+'%') AND {2} AND A.ID NOT IN(SELECT TOP {1} B.ID FROM 购件目录 B WHERE B.状态=0 AND (@P0='' OR B.代号 LIKE @P0+'%') AND {3} ORDER BY B.代号) ORDER BY A.代号", iPageSize, iPageSize * (iPageIndex - 1), BGlobal.BuildLike(sWhere, "A.代号", "A.名称", "A.规格型号"), BGlobal.BuildLike(sWhere, "B.代号", "B.名称", "B.规格型号")), sDh);
            }
        }

        /// <summary>
        /// 根据[编辑人]获得外购件
        /// </summary>
        public static DataSet GetWgjByBjr(string sBjr)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                return da.ExecuteQuery("SELECT TOP 25 A.ID,A.代号,A.类别,A.名称,A.规格型号,A.单位,A.品牌厂家,A.编辑人,(SELECT COUNT(1) FROM 购件文档 B WHERE A.代号 LIKE B.购件代号+'%') 文档 FROM 购件目录 A WHERE A.状态=0 AND (@P0='' OR A.编辑人=@P0) ORDER BY A.ID DESC", sBjr);
            }
        }

        /// <summary>
        /// 保存外购件
        /// </summary>
        public static string SaveWgj(string sDh, string sLb, string sMc, string sGgxh, string sDw, string sPpcj, string sBjr)
        {
            if (sDh == string.Empty || sMc.Trim() == string.Empty || sDw.Trim() == string.Empty)
            {
                return "外购件的代号、名称、计量单位不能为空！";
            }
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                if (da.Execute("INSERT INTO 购件目录(代号,类别,名称,规格型号,单位,品牌厂家,备注,编辑人,编辑时间,状态) SELECT @P0+'-'+REPLACE(STR(ISNULL(MAX(RIGHT(代号,@P8)),0)+1,@P8),' ','0'),@P1,@P2,@P3,@P4,@P5,'',@P6,@P7,0 FROM 购件目录 WHERE 代号 LIKE @P0+'%'", sDh, sLb, sMc.Trim(), sGgxh.Trim(), sDw.Trim(), sPpcj.Trim(), sBjr, DateTime.Now, (sDh.Length <= 15 ? 4 : 20 - 1 - sDh.Length)) <= 0)
                {
                    return "保存外购件失败！";
                }
                return null;
            }
        }

        /// <summary>
        /// 根据[外购件ID]删除外购件
        /// </summary>
        public static string DelWgj(int iWgjId)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                if (da.Execute("DELETE FROM 购件目录 WHERE ID=@P0 AND 状态=0 AND NOT EXISTS(SELECT 1 FROM 购件文档 WHERE 购件文档.购件代号=购件目录.代号) AND NOT EXISTS(SELECT 1 FROM 产品外购 WHERE 产品外购.购件ID=购件目录.ID) AND NOT EXISTS(SELECT 1 FROM 物资入库 WHERE 物资入库.物资编号=购件目录.代号)", iWgjId) <= 0)
                {
                    return "删除外购件失败，可能存在引用！";
                }
                return null;
            }
        }

        /// <summary>
        /// 根据[外购件ID]获得外购件编辑
        /// </summary>
        public static DataSet GetWgjBjById(int iWgjId)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                return da.ExecuteQuery("SELECT ID,代号,类别,名称,规格型号,单位,品牌厂家,备注,编辑人,编辑时间 FROM 购件目录 WHERE 状态=0 AND ID=@P0", iWgjId);
            }
        }

        /// <summary>
        /// 根据[外购件代号]获得外购件文档
        /// </summary>
        public static DataSet GetWgjWdByWgjDh(string sWgjDh)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                return da.ExecuteQuery("SELECT ID,购件代号,文档名称,备注,路径,编辑人,编辑时间 FROM 购件文档 WHERE @P0 LIKE 购件代号+'%' ORDER BY ID", sWgjDh);
            }
        }

        /// <summary>
        /// 保存外购件文档
        /// </summary>
        public static string SaveWgjWd(int iWgjId, string sDh, string sMc, string sGgxh, string sDw, string sPpcj, string sBz, string sBjr, GridView gWgjWd)
        {
            if (sDh == string.Empty || sMc.Trim() == string.Empty || sDw.Trim() == string.Empty)
            {
                return "外购件的代号、名称、计量单位不能为空！";
            }
            for (int i = 0; i < gWgjWd.Rows.Count; i++)
            {
                if (((TextBox)gWgjWd.Rows[i].Cells[1].Controls[1]).Text.Trim() == string.Empty && gWgjWd.DataKeys[i].Value.ToString() != string.Empty)
                {
                    return "外购件文档的名称不能为空！";
                }
            }
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                if (da.Execute("UPDATE 购件目录 SET 名称=@P1,规格型号=@P2,单位=@P3,品牌厂家=@P4,备注=@P5,编辑人=@P6,编辑时间=@P7 WHERE ID=@P0 AND 状态=0 AND (名称<>@P1 OR 规格型号<>@P2 OR 单位<>@P3 OR 品牌厂家<>@P4 OR 备注<>@P5)", iWgjId, sMc.Trim(), sGgxh.Trim(), sDw.Trim(), sPpcj.Trim(), sBz.Trim(), sBjr, DateTime.Now) < 0)
                {
                    return "保存外购件文档失败！";
                }
                string[] sFilePathArr = new string[gWgjWd.Rows.Count];
                for (int i = 0; i < gWgjWd.Rows.Count; i++)
                {
                    FileUpload fUpload = (FileUpload)gWgjWd.Rows[i].Cells[1].Controls[3];
                    if (fUpload.PostedFile == null || fUpload.PostedFile.FileName == string.Empty || ((TextBox)gWgjWd.Rows[i].Cells[1].Controls[1]).Text.Trim() == string.Empty)
                    {
                        sFilePathArr[i] = gWgjWd.DataKeys[i].Values[1].ToString();
                    }
                    else
                    {
                        string sFileKzm = (fUpload.PostedFile.FileName.LastIndexOf(".") > 0) ? BGlobal.StringToHex(fUpload.PostedFile.FileName.Substring(fUpload.PostedFile.FileName.LastIndexOf(".") + 1).ToLower()) : string.Empty;
                        sFilePathArr[i] = string.Format("Wg{0}{1}_{2}", iWgjId.ToString("X5"), DateTime.Now.ToString("yyyyMMddHHmmssffffff"), sFileKzm);
                        try
                        {
                            fUpload.PostedFile.SaveAs(string.Format("{0}{1}", BGlobal.AttachmentPath, sFilePathArr[i]));
                        }
                        catch
                        {
                            sFilePathArr[i] = gWgjWd.DataKeys[i].Values[1].ToString();
                        }
                    }
                    if (gWgjWd.DataKeys[i].Value.ToString() != string.Empty)
                    {
                        da.Execute("UPDATE 购件文档 SET 购件代号=@P1,文档名称=@P2,备注=@P3,路径=@P4,编辑人=@P5,编辑时间=@P6 WHERE ID=@P0 AND (购件代号<>@P1 OR 文档名称<>@P2 OR 备注<>@P3 OR 路径<>@P4)", (int)gWgjWd.DataKeys[i].Value, (((CheckBox)gWgjWd.Rows[i].Cells[3].Controls[1]).Checked ? sDh.Substring(0, sDh.IndexOf("-")) : sDh), ((TextBox)gWgjWd.Rows[i].Cells[1].Controls[1]).Text.Trim(), ((TextBox)gWgjWd.Rows[i].Cells[2].Controls[1]).Text.Trim(), sFilePathArr[i], sBjr, DateTime.Now);
                    }
                    else if (sFilePathArr[i] != string.Empty)
                    {
                        da.Execute("INSERT INTO 购件文档(购件代号,文档名称,备注,路径,编辑人,编辑时间) VALUES(@P0,@P1,@P2,@P3,@P4,@P5)", (((CheckBox)gWgjWd.Rows[i].Cells[3].Controls[1]).Checked ? sDh.Substring(0, sDh.IndexOf("-")) : sDh), ((TextBox)gWgjWd.Rows[i].Cells[1].Controls[1]).Text.Trim(), ((TextBox)gWgjWd.Rows[i].Cells[2].Controls[1]).Text.Trim(), sFilePathArr[i], sBjr, DateTime.Now);
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// 根据[外购件文档ID]删除外购件文档
        /// </summary>
        public static string DelWgjWd(int iWgjWdId)
        {
            using (DSql da = new DSql(BGlobal.DbConnString))
            {
                if (da.Execute("DELETE FROM 购件文档 WHERE ID=@P0 AND EXISTS(SELECT 1 FROM 购件目录 WHERE 购件目录.代号 LIKE 购件文档.购件代号+'%' AND 购件目录.状态=0)", iWgjWdId) <= 0)
                {
                    return "删除外购件文档失败，可能已提交！";
                }
                return null;
            }
        }

        #endregion
    }
}
